<!DOCTYPE html>
<html lang="zh-CN">
  <head>
  <meta charset="UTF-8">
  <meta 
    name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
  <meta 
    http-equiv="X-UA-Compatible" 
    content="ie=edge">
  <meta 
    name="theme-color" 
    content="#fff" 
    id="theme-color">
  <meta 
    name="description" 
    content="程序员的快乐小网站">
  <link 
    rel="icon" 
    href="https://demo.theme-kaze.top/img/Kaze.png">
  <title>python爬取淘宝商品</title>
  
    
      <meta 
        property="og:title" 
        content="python爬取淘宝商品">
    
    
      <meta 
        property="og:url" 
        content="http://www.baikaihua.gitee.io/2021/12/13/python%E7%88%AC%E5%8F%96%E6%B7%98%E5%AE%9D%E5%95%86%E5%93%81/index.html">
    
    
      <meta 
        property="og:img" 
        content="https://gitee.com/baikaihua/tochang/raw/master/%E5%9B%BE%E7%89%871.png">
    
    
      <meta 
        property="og:img" 
        content="&lt;p&gt;这篇博客来自我的作业。&lt;/p&gt;
&lt;p&gt;作者：白开画&lt;/p&gt;">
    
    
      <meta 
        property="og:type" 
        content="article">
      <meta 
        property="og:article:published_time" 
        content="2021-12-13">
      <meta 
        property="og:article:modified_time" 
        content="2021-12-13">
      <meta 
        property="og:article:author" 
        content="白开画 and 徐威">
      
        
          <meta 
            property="og:article:tag" 
            content="白开画">
        
          <meta 
            property="og:article:tag" 
            content="python">
        
          <meta 
            property="og:article:tag" 
            content="MySQL">
        
          <meta 
            property="og:article:tag" 
            content="爬虫">
        
          <meta 
            property="og:article:tag" 
            content="淘宝">
        
      
    
  
  <script>
    function loadScript(url, cb) {
      var script = document.createElement('script');
      script.src = url;
      if (cb) script.onload = cb;
      script.async = true;
      document.body.appendChild(script);
    }
    function loadCSS(href, data, attr) {
      var sheet = document.createElement('link');
      sheet.ref = 'stylesheet';
      sheet.href = href;
      sheet.dataset[data] = attr;
      document.head.appendChild(sheet);
    }
    function changeCSS(cssFile, data, attr) {
      var oldlink = document.querySelector(data);
      var newlink = document.createElement("link");
      newlink.setAttribute("rel", "stylesheet");
      newlink.setAttribute("href", cssFile);
      newlink.dataset.prism = attr;
      document.head.replaceChild(newlink, oldlink);
    }
  </script>
  
    
  
  <script>
    // control reverse button
    var reverseDarkList = {
      dark: 'light',
      light: 'dark'
    };
    var themeColor = {
      dark: '#1c1c1e',
      light: '#fff'
    }
    // get the data of css prefers-color-scheme
    var getCssMediaQuery = function() {
      return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
    };
    // reverse current darkmode setting function
    var reverseDarkModeSetting = function() {
      var setting = localStorage.getItem('user-color-scheme');
      if(reverseDarkList[setting]) {
        setting = reverseDarkList[setting];
      } else if(setting === null) {
        setting = reverseDarkList[getCssMediaQuery()];
      } else {
        return;
      }
      localStorage.setItem('user-color-scheme', setting);
      return setting;
    };
    // apply current darkmode setting
  </script>
  
    <script>
      var setDarkmode = function(mode) {
      var setting = mode || localStorage.getItem('user-color-scheme');
      if(setting === getCssMediaQuery()) {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
      } else if(reverseDarkList[setting]) {
        document.documentElement.setAttribute('data-user-color-scheme', setting);
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
      } else {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[getCssMediaQuery()];
        document.getElementById('theme-color').dataset.mode = getCssMediaQuery();
      }
    };
    setDarkmode();
    </script>
  
  
  <link rel="preload" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css" as="style" >
  <link rel="preload" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css" as="style" >
  
  
    <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.js" as="script">
    <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.css" as="style" >
  
  
    <link rel="preload" href="/js/lib/lozad.min.js" as="script">
  
  
  
  
    
    <link rel="prefetch" href="//cdn.jsdelivr.net/npm/katex@0.11.1/dist/katex.min.css" as="script">
  
  
    
    <link rel="prefetch" href="//unpkg.com/valine/dist/Valine.min.js" as="script">
  
  
  
  <link rel="stylesheet" href="/css/main.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css">
  
    <link rel="stylesheet" href="/js/lib/lightbox/baguetteBox.min.css">
  
<meta name="generator" content="Hexo 5.4.0"></head>

  <body>
    <div class="wrapper">
       
      <nav class="navbar">
  <div class="navbar-logo">
    <span class="navbar-logo-main">
      
        <img 
          class="navbar-logo-img" 
          src="https://demo.theme-kaze.top/img/Kaze.png" 
          alt="blog logo">
      
      <span class="navbar-logo-dsc">程序小站</span>
    </span>
  </div>
  <div class="navbar-menu">
    
      <a 
        href="/" 
        class="navbar-menu-item">
        
          首页
        
      </a>
    
      <a 
        href="/archives" 
        class="navbar-menu-item">
        
          归档
        
      </a>
    
      <a 
        href="/tags" 
        class="navbar-menu-item">
        
          标签
        
      </a>
    
      <a 
        href="/categories" 
        class="navbar-menu-item">
        
          分类
        
      </a>
    
      <a 
        href="/about" 
        class="navbar-menu-item">
        
          关于
        
      </a>
    
    <a 
      class="navbar-menu-item darknavbar" 
      id="dark">
      <i class="iconfont icon-weather"></i>
    </a>
    <a 
      class="navbar-menu-item searchnavbar" 
      id="search">
      <i 
        class="iconfont icon-search" 
        style="font-size: 1.2rem; font-weight: 400;">
      </i>
    </a>
  </div>
</nav> 
      
      <div 
        id="local-search" 
        style="display: none">
        <input
          class="navbar-menu-item"
          id="search-input"
          placeholder="请输入搜索内容..." />
        <div id="search-content"></div>
      </div>
      
      <div class="section-wrap">
        <div class="container">
          <div class="columns">
            <main class="main-column">
  <div class="image-wrapper">
    <img 
      src="https://gitee.com/baikaihua/tochang/raw/master/%E5%9B%BE%E7%89%871.png" 
      data-src="https://gitee.com/baikaihua/tochang/raw/master/%E5%9B%BE%E7%89%871.png"
      srcset="data:image/svg+xml,%3Csvg%20xmlns=&#39;http://www.w3.org/2000/svg&#39;%20viewBox=&#39;0%200%20300%20300&#39;%3E%3C/svg%3E"
      class="image lozad"
      alt="python爬取淘宝商品 thumbnail">
  </div>

<article class="card card-content">
  <header>
    <h1 class="post-title">
      python爬取淘宝商品
    </h1>
  </header>
  <div class="post-meta post-show-meta">
    <time datetime="2021-12-13T15:08:40.164Z">
      <i 
        class="iconfont icon-calendar" 
        style="margin-right: 2px;">
      </i>
      <span>2021-12-13</span>
    </time>
    
      <span class="dot"></span>
      
        <a 
          href="/categories/python/" 
          class="post-meta-link">
          python
        </a>
      
    
    
      <span class="dot"></span>
      <span>3.7k 字</span>
    
  </div>
  
    <div 
      class="post-meta post-show-meta" 
      style="margin-top: -10px;">
      <div style="display: flex; align-items: center;">
        <i 
          class="iconfont icon-biaoqian" 
          style="margin-right: 2px; font-size: 1.15rem;">
        </i>
        
          
          <a 
            href="/tags/%E7%99%BD%E5%BC%80%E7%94%BB/" 
            class="post-meta-link">
            白开画
          </a>
        
          
            <span class="dot"></span>
          
          <a 
            href="/tags/python/" 
            class="post-meta-link">
            python
          </a>
        
          
            <span class="dot"></span>
          
          <a 
            href="/tags/MySQL/" 
            class="post-meta-link">
            MySQL
          </a>
        
          
            <span class="dot"></span>
          
          <a 
            href="/tags/%E7%88%AC%E8%99%AB/" 
            class="post-meta-link">
            爬虫
          </a>
        
          
            <span class="dot"></span>
          
          <a 
            href="/tags/%E6%B7%98%E5%AE%9D/" 
            class="post-meta-link">
            淘宝
          </a>
        
      </div>
    </div>
  
  </header>
  <div 
    id="section" 
    class="post-content">
    <p>这篇博客来自我的作业。</p>
<p>作者：白开画</p>
<span id="more"></span>
<h3 id="实验内容"><a class="markdownIt-Anchor" href="#实验内容"></a> 实验内容</h3>
<p>​	使用python的requests库来编写爬虫程序，爬取淘宝指定商品的信息到数据库。</p>
<h3 id="实验目的"><a class="markdownIt-Anchor" href="#实验目的"></a> 实验目的</h3>
<ol>
<li>理解python 爬虫的工作流程</li>
<li>了解request库和pymysql库的简单使用</li>
<li>实现简单的数据分析与提取</li>
</ol>
<h3 id="实验环境"><a class="markdownIt-Anchor" href="#实验环境"></a> 实验环境</h3>
<ol>
<li>MySQL 5.5.62</li>
<li>Python 3.8</li>
<li>Navicat for MySQL</li>
</ol>
<h3 id="实验任务列表"><a class="markdownIt-Anchor" href="#实验任务列表"></a> 实验任务列表</h3>
<ol>
<li>安装request库和pymysql库</li>
<li>分析淘宝URL</li>
<li>爬取淘宝HTML</li>
<li>提取所需信息</li>
<li>爬取商品HTML</li>
<li>提取商品信息</li>
<li>将商品信息导入到数据库</li>
</ol>
<h3 id="实验过程"><a class="markdownIt-Anchor" href="#实验过程"></a> 实验过程</h3>
<h4 id="安装request库和pymysql库"><a class="markdownIt-Anchor" href="#安装request库和pymysql库"></a> 安装request库和pymysql库</h4>
<p>Request库：</p>
<p>​		requests是用python语言基于urllib编写的，采用的是Apache2 Licensed开源协议的HTTP库，可以为我们实现简单的HTTP服务。</p>
<p>​		我们将用request库来实现网页的爬取。</p>
<p>Pymysql库：</p>
<p>​		Pymysql是python操作MySQL数据库常用的库，可以实现数据的增、删、改、查等操作。</p>
<p>​		我们用这个库连接MySQL来把爬取的数据进行存储。</p>
<p>​		库的安装用到的是python的pip工具，在Windows的命令提示符输入命令安装</p>
<p>​		命令：</p>
<p>​	<strong>pip install requests</strong> 	//安装request库</p>
<p>​	<strong>pip install pymysql</strong> 	//安装pymysql库</p>
<p><img src="https://gitee.com/baikaihua/tochang/raw/master/2.jpg" alt="" / srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="https://gitee.com/baikaihua/tochang/raw/master/2.jpg" class="lozad post-image"></p>
<p><img src="https://gitee.com/baikaihua/tochang/raw/master/3.png" alt="" / srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="https://gitee.com/baikaihua/tochang/raw/master/3.png" class="lozad post-image"></p>
<p>​		出现上图所示内容则安装成功。</p>
<p>​		用pip工具安装库时可能会出现如下图错误</p>
<p><img src="https://gitee.com/baikaihua/tochang/raw/master/4.jpg" alt="" / srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="https://gitee.com/baikaihua/tochang/raw/master/4.jpg" class="lozad post-image"></p>
<p>请看我往期博客<a target="_blank" rel="noopener" href="https://baikaihua.gitee.io/2021/08/31/%E2%80%99pip%E4%B8%8D%E6%98%AF%E5%86%85%E9%83%A8%E6%88%96%E5%A4%96%E9%83%A8%E5%91%BD%E4%BB%A4%EF%BC%8C%E4%B9%9F%E4%B8%8D%E6%98%AF%E5%8F%AF%E8%BF%90%E8%A1%8C%E7%A8%8B%E5%BA%8F%E2%80%98%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88/index.html">‘pip不是内部或外部命令，也不是可运行程序’解决方案 (gitee.io)</a></p>
<h4 id="淘宝的url分析"><a class="markdownIt-Anchor" href="#淘宝的url分析"></a> 淘宝的URL分析</h4>
<p>​		人们用淘宝搜索想要的东西后，需要点击页面下方的翻页按钮来让页面显示更多的商品信息。</p>
<p><img src="https://gitee.com/baikaihua/tochang/raw/master/QQ%E6%88%AA%E5%9B%BE20211207212447.png" alt="" / srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="https://gitee.com/baikaihua/tochang/raw/master/QQ%E6%88%AA%E5%9B%BE20211207212447.png" class="lozad post-image"></p>
<p>​		同样让页面进行翻页这个动作，我们也可以通过改变淘宝URL来进行。</p>
<p>​		第一页：<a target="_blank" rel="noopener" href="https://s.taobao.com/search?q=%E6%89%8B%E6%9C%BA&amp;s=0">https://s.taobao.com/search?q=手机&amp;s=0</a></p>
<p>​		第二页：<a target="_blank" rel="noopener" href="https://s.taobao.com/search?q=%E6%89%8B%E6%9C%BA&amp;s=44">https://s.taobao.com/search?q=手机&amp;s=44</a></p>
<p>​		第三页：<a target="_blank" rel="noopener" href="https://s.taobao.com/search?q=%E6%89%8B%E6%9C%BA&amp;s=88">https://s.taobao.com/search?q=手机&amp;s=88</a></p>
<p>​		我们通过观察URL很容易看出q后面的参数就是我们要获取的物品的名字，s后面的参数是分页的依据，最后s的值=（44（页数-1））。</p>
<p>​		我们用上面分析得到的规律对爬虫程序中的URL进行构建。</p>
<pre class="highlight"><span class="line">goods = <span class="string">&quot;手机&quot;</span>    <span class="comment">#设置爬取的商品</span></span><br><span class="line">   depth = <span class="number">1</span>         <span class="comment">#设置爬取的页数</span></span><br><span class="line">   start_url = <span class="string">&quot;https://s.taobao.com/search?q=&quot;</span> + goods</span><br><span class="line">url = start_url + <span class="string">&quot;&amp;s=&quot;</span> + <span class="built_in">str</span>(<span class="number">44</span> * i)    <span class="comment">#构建搜索页URL</span></span><br></pre>
<h4 id="获取页面信息模块"><a class="markdownIt-Anchor" href="#获取页面信息模块"></a> 获取页面信息模块</h4>
<pre class="highlight"><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">getHTMLText</span>(<span class="params">url</span>):</span>      <span class="comment">#爬取淘宝HTML</span></span><br><span class="line">    <span class="keyword">try</span>:</span><br><span class="line">        r = requests.get(url, timeout=<span class="number">30</span>) <span class="comment">#timeout设置一个返回时间,如果在这个时间之内请求没有返回,就返回一个TimeErro</span></span><br><span class="line">        r.raise_for_status()            <span class="comment">#判断网页是否访问正常</span></span><br><span class="line">        r.encoding = r.apparent_encoding <span class="comment">#将网页的编码实际分析值传递给猜测值</span></span><br><span class="line">        <span class="keyword">return</span> r.text</span><br><span class="line">    <span class="keyword">except</span>:</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;网页访问失败&quot;</span>)</span><br></pre>
<h4 id="请求头与cooker"><a class="markdownIt-Anchor" href="#请求头与cooker"></a> 请求头与cooker</h4>
<p>​		我们在爬虫的时候，主要是想要获取对面的数据，而对面则是不想让我们进行获取。所以我们会进行各种各样的伪装，请求头和cookie都是让我们伪装成浏览器，让对方识别我们为爬虫的概率更小，让我们爬取到想要的数据。</p>
<p>​		Cookie每隔一段时间或者当访问过于频繁的时候会失效，需要在失效后更换。</p>
<pre class="highlight"><span class="line">headers = &#123;            <span class="comment">#user-agent是请求头，模仿浏览器访问淘宝；cookie是身份令牌</span></span><br><span class="line">              <span class="string">&quot;user-agent&quot;</span>:<span class="string">&quot;Mozilla/5.0( WindowsNT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36&quot;</span>,</span><br><span class="line"><span class="string">&quot;cookie&quot;</span>: “这里需要填入cookie”</span><br><span class="line">&#125;</span><br></pre>
<h4 id="建立网页连接"><a class="markdownIt-Anchor" href="#建立网页连接"></a> 建立网页连接</h4>
<p>​	进行过3和4的准备后，我们就可以与淘宝网页建立连接了。</p>
<pre class="highlight"><span class="line">html = requests.get(url, headers=headers)    <span class="comment">#与网页建立连接</span></span><br></pre>
<h4 id="分析淘宝搜索后的html"><a class="markdownIt-Anchor" href="#分析淘宝搜索后的html"></a> 分析淘宝搜索后的HTML</h4>
<p>​		在搜索某一件商品后，每一页页面里琳琅满目有几十个商品，我们的实验目的是一一获取这些商品的详细数据，但这个页面所提供的信息过于简略，不足于满足我们的要求。</p>
<p>​		我们需要进入商品的详细信息页面，然后去提取我们想要的信息。</p>
<p>​		页面的跳转我们可以通过构建URL，再次进行链接来进行转换</p>
<p>​		<a target="_blank" rel="noopener" href="https://detail.tmall.com/item.htm?id=634123043849">https://detail.tmall.com/item.htm?id=634123043849</a></p>
<p>​		通过分析某一商品详细页面的URL，可以看出由两部分组成</p>
<ol>
<li>
<p><a target="_blank" rel="noopener" href="https://detail.tmall.com/item.htm">https://detail.tmall.com/item.htm</a>?</p>
</li>
<li>
<p>d=634123043849</p>
<p>只要我们获取每一个商品的id号就可以构建出每一个商品的URL。</p>
</li>
</ol>
<p>搜索页面中商品的id号都可以在搜索页面的HTML中找到。</p>
<p>​		我们可以通过正则表达式对HTML中想要的数据进行提取，正则表达式所需的库是re，因为re是python自带的库，所以不需要通过pip工具进行安装就可以使用。</p>
<pre class="highlight"><span class="line"> b=re.findall(<span class="string">r&#x27;\&quot;auctionNids\&quot;\:\[(.*?)\]&#x27;</span>,html.text)   <span class="comment">#提取某页所有商品的id，用于爬取商品的详细属性</span></span><br><span class="line"> c=<span class="built_in">str</span>(b)</span><br><span class="line">b=re.findall(<span class="string">r&#x27;\d+&#x27;</span>,c)</span><br></pre>
<h4 id="url的二次构建与连接"><a class="markdownIt-Anchor" href="#url的二次构建与连接"></a> URL的二次构建与连接</h4>
<pre class="highlight"><span class="line">start_url_1 = <span class="string">&quot;https://detail.tmall.com/item.htm?id=&quot;</span></span><br><span class="line">url_1 = start_url_1 + b[n]     <span class="comment">#根据前面爬取的商品标志构建URL，更换页面，爬取商品详细属性</span></span><br><span class="line">html_1 = requests.get(url_1, headers=headers) </span><br></pre>
<h4 id="商品详细页面html的分析与数据提取"><a class="markdownIt-Anchor" href="#商品详细页面html的分析与数据提取"></a> 商品详细页面HTML的分析与数据提取</h4>
<p>​	用request.get方法对商品详细页面的HTML进行了爬取后，我们需要在HTML页面中提取出商品的详细规格参数。</p>
<p><img src="https://gitee.com/baikaihua/tochang/raw/master/QQ%E6%88%AA%E5%9B%BE20211207223722.png" alt="" / srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="https://gitee.com/baikaihua/tochang/raw/master/QQ%E6%88%AA%E5%9B%BE20211207223722.png" class="lozad post-image"></p>
<p><img src="https://gitee.com/baikaihua/tochang/raw/master/QQ%E6%88%AA%E5%9B%BE20211213163745.png" alt="" / srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="https://gitee.com/baikaihua/tochang/raw/master/QQ%E6%88%AA%E5%9B%BE20211213163745.png" class="lozad post-image"></p>
<p>​		通过观察页面的源代码，我们可以看出，需要再次使用正则表达式对想要的数据进行提取。</p>
<p>​		通过构建parsePage_1(html_1)来对数据进行提取</p>
<pre class="highlight"><span class="line">a=parsePage_1(html_1.text)       <span class="comment">#将爬取的商品详尽页传入parsePage_1，用于提取商品属性，并返回到a</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">parsePage_1</span>(<span class="params">html_1</span>):</span></span><br><span class="line">    <span class="keyword">try</span>:</span><br><span class="line">        plt_1 = re.findall(<span class="string">r&#x27;\&gt;后置摄像头\&lt;\/th\&gt;\&lt;td\&gt;\&amp;nbsp\;(.*?)\&lt;&#x27;</span>,html_1)     <span class="comment">#用正则表达式对所需数据进行提取</span></span><br><span class="line">        plt_2 = re.findall(<span class="string">r&#x27;\&gt;分辨率\&lt;\/th\&gt;\&lt;td\&gt;\&amp;nbsp\;(.*?)\&lt;&#x27;</span>,html_1)</span><br><span class="line">        plt_3 = re.findall(<span class="string">r&#x27;\&gt;品牌\&lt;\/th\&gt;\&lt;td\&gt;\&amp;nbsp\;(.*?)\&lt;&#x27;</span>,html_1)</span><br><span class="line">        plt_4 = re.findall(<span class="string">r&#x27;\&gt;产品名称\&lt;\/th\&gt;\&lt;td\&gt;\&amp;nbsp\;(.*?)\&lt;&#x27;</span>,html_1)</span><br><span class="line">        plt_5 = re.findall(<span class="string">r&#x27;\&gt;CPU品牌\&lt;\/th\&gt;\&lt;td\&gt;\&amp;nbsp\;(.*?)\&lt;&#x27;</span>,html_1)</span><br><span class="line">        <span class="keyword">if</span> (plt_1==[]): plt_1=[<span class="string">&#x27;数据不存在&#x27;</span>]     <span class="comment">#处理提取不到的情况</span></span><br><span class="line">        <span class="keyword">if</span> (plt_2==[]): plt_2=[<span class="string">&#x27;数据不存在&#x27;</span>]</span><br><span class="line">        <span class="keyword">if</span> (plt_3==[]): plt_3=[<span class="string">&#x27;数据不存在&#x27;</span>]</span><br><span class="line">        <span class="keyword">if</span> (plt_4==[]): plt_4=[<span class="string">&#x27;数据不存在&#x27;</span>]</span><br><span class="line">        <span class="keyword">if</span> (plt_5==[]): plt_5=[<span class="string">&#x27;数据不存在&#x27;</span>]</span><br><span class="line">        a=plt_3+plt_4+plt_5+plt_1+plt_2</span><br><span class="line">        <span class="keyword">return</span> a</span><br><span class="line">    <span class="keyword">except</span> Exception <span class="keyword">as</span> e:</span><br><span class="line">        <span class="built_in">print</span>(e)</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;信息提取失败&quot;</span>)</span><br><span class="line">通上述代码我们把想要的数据提取出来并且保存在数列a中。</span><br></pre>
<h4 id="mysql数据库的连接与关闭"><a class="markdownIt-Anchor" href="#mysql数据库的连接与关闭"></a> MySQL数据库的连接与关闭</h4>
<h5 id="连接"><a class="markdownIt-Anchor" href="#连接"></a> 连接</h5>
<p>​		我们的爬虫程序爬取的数据储存到数据库需要用到pymysql库，因为已经安装了这个库，只需要在代码的最前面引用即可</p>
<pre class="highlight"><span class="line">	<span class="keyword">import</span> pymysql</span><br><span class="line">	<span class="keyword">try</span>:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;正在与数据库建立连接...&quot;</span>)</span><br><span class="line">    db = pymysql.connect( host=<span class="string">&quot;localhost&quot;</span>,user=<span class="string">&quot;root&quot;</span>,passwd=<span class="string">&quot;root&quot;</span>,db=<span class="string">&quot;taobao&quot;</span>,charset=<span class="string">&quot;utf8&quot;</span>)   <span class="comment">#与本地数据库建立连接，db连接数据库taobao，charset连接后使用的编码为utf8</span></span><br><span class="line">    cursor = db.cursor()            <span class="comment">#获取游标对象</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;数据库连接成功！&quot;</span>)</span><br><span class="line"><span class="keyword">except</span> Exception <span class="keyword">as</span> e:</span><br><span class="line">    <span class="built_in">print</span>(e)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;数据库无法连接!&quot;</span>)</span><br></pre>
<p>​		代码pymysql.connect的host参数我们也可以设置为具体的IP地址，因为我们连接的是我们的本地，所以我写成了localhost，需要特别注意的是charset参数一定要与MySQL数据库的编码保持一致。</p>
<h5 id="关闭"><a class="markdownIt-Anchor" href="#关闭"></a> 关闭</h5>
<pre class="highlight"><span class="line"><span class="keyword">try</span>:</span><br><span class="line">    db.close()    <span class="comment">#关闭数据库连接</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;数据库已关闭&quot;</span>)</span><br><span class="line"><span class="keyword">except</span> Exception <span class="keyword">as</span> e:</span><br><span class="line">    <span class="built_in">print</span>(e)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;数据库关闭失败！&quot;</span>)</span><br></pre>
<h4 id="数据库的准备"><a class="markdownIt-Anchor" href="#数据库的准备"></a> 数据库的准备</h4>
<p>​		对爬取的数据进行保存之前我们需要在MySQL里面建好数据库和表，并且所建表的编码必须要和连接时charset参数保持一致。</p>
<pre class="highlight"><span class="line">CRTATE DATABASE taobao;</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> sj(</span><br><span class="line">id <span class="type">int</span>(<span class="number">4</span>) <span class="keyword">not</span> <span class="keyword">null</span>,</span><br><span class="line">pp <span class="type">varchar</span>(<span class="number">200</span>)  <span class="type">character</span> <span class="keyword">set</span> utf8,</span><br><span class="line">cpmc <span class="type">varchar</span>(<span class="number">200</span>) <span class="type">character</span> <span class="keyword">set</span> utf8,</span><br><span class="line">cpu <span class="type">varchar</span>(<span class="number">200</span>) <span class="type">character</span> <span class="keyword">set</span> utf8,</span><br><span class="line">hzsxt <span class="type">varchar</span>(<span class="number">200</span>) <span class="type">character</span> <span class="keyword">set</span> utf8,</span><br><span class="line">fbl <span class="type">varchar</span>(<span class="number">200</span>) <span class="type">character</span> <span class="keyword">set</span> utf8</span><br><span class="line">)<span class="keyword">DEFAULT</span> CHARSET<span class="operator">=</span>utf8;</span><br></pre>
<p>​		我们创建的一个名为taobao的数据库，并在库中创建的一个名为sj的表。</p>
<h4 id="数据的插入"><a class="markdownIt-Anchor" href="#数据的插入"></a> 数据的插入</h4>
<p>​		在数据插入之前我们需要对SQL语句进行构造，代码如下：</p>
<pre class="highlight"><span class="line"><span class="keyword">try</span>:</span><br><span class="line">    </span><br><span class="line">    sql = <span class="string">&quot;INSERT INTO sj(id,pp,cpmc,cpu,hzsxt,fbl)VALUES (%s,&#x27;%s&#x27;,&#x27;%s&#x27;,&#x27;%s&#x27;,&#x27;%s&#x27;,&#x27;%s&#x27;)&quot;</span>%(n,a[<span class="number">0</span>],a[<span class="number">1</span>],a[<span class="number">2</span>],a[<span class="number">3</span>],a[<span class="number">4</span>])    <span class="comment">#构建SQL语句，为导入到数据库做准备</span></span><br><span class="line">    cursor.execute(sql)        <span class="comment">#开始插入数据</span></span><br><span class="line">    db.commit()             <span class="comment">#保存</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;数据已写入数据库&quot;</span>)</span><br><span class="line"><span class="keyword">except</span> Exception <span class="keyword">as</span> e:</span><br><span class="line">    <span class="built_in">print</span> (e)</span><br><span class="line">    db.rollback()    <span class="comment">#如果发送错误进行数据回滚</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;数据库写入失败！&quot;</span>)</span><br></pre>
<p>​		SQL语句构造完成后用cursor.execute（）来把语句传到MySQL中并执行。</p>
<p>​		db.commit()语句是用来对执行结果的保存，db.rollback()是在执行失败后对数据进行混滚。</p>
<h4 id="全部代码"><a class="markdownIt-Anchor" href="#全部代码"></a> 全部代码</h4>
<pre class="highlight"><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">import</span> re</span><br><span class="line"><span class="keyword">import</span> pymysql</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">getHTMLText</span>(<span class="params">url</span>):</span></span><br><span class="line">    <span class="keyword">try</span>:</span><br><span class="line">        r = requests.get(url, timeout=<span class="number">30</span>)</span><br><span class="line">        r.raise_for_status()</span><br><span class="line">        r.encoding = r.apparent_encoding</span><br><span class="line">        <span class="keyword">return</span> r.text</span><br><span class="line"></span><br><span class="line">    <span class="keyword">except</span>:</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;网页访问失败&quot;</span>)</span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">parsePage_1</span>(<span class="params">html_1</span>):</span></span><br><span class="line">    <span class="keyword">try</span>:</span><br><span class="line">        plt_1 = re.findall(<span class="string">r&#x27;\&gt;后置摄像头\&lt;\/th\&gt;\&lt;td\&gt;\&amp;nbsp\;(.*?)\&lt;&#x27;</span>,html_1)     <span class="comment">#用正则表达式对所需数据进行提取</span></span><br><span class="line">        plt_2 = re.findall(<span class="string">r&#x27;\&gt;分辨率\&lt;\/th\&gt;\&lt;td\&gt;\&amp;nbsp\;(.*?)\&lt;&#x27;</span>,html_1)</span><br><span class="line">        plt_3 = re.findall(<span class="string">r&#x27;\&gt;品牌\&lt;\/th\&gt;\&lt;td\&gt;\&amp;nbsp\;(.*?)\&lt;&#x27;</span>,html_1)</span><br><span class="line">        plt_4 = re.findall(<span class="string">r&#x27;\&gt;产品名称\&lt;\/th\&gt;\&lt;td\&gt;\&amp;nbsp\;(.*?)\&lt;&#x27;</span>,html_1)</span><br><span class="line">        plt_5 = re.findall(<span class="string">r&#x27;\&gt;CPU品牌\&lt;\/th\&gt;\&lt;td\&gt;\&amp;nbsp\;(.*?)\&lt;&#x27;</span>,html_1)</span><br><span class="line">        <span class="keyword">if</span> (plt_1==[]): plt_1=[<span class="string">&#x27;数据不存在&#x27;</span>]     <span class="comment">#处理提取不到的情况</span></span><br><span class="line">        <span class="keyword">if</span> (plt_2==[]): plt_2=[<span class="string">&#x27;数据不存在&#x27;</span>]</span><br><span class="line">        <span class="keyword">if</span> (plt_3==[]): plt_3=[<span class="string">&#x27;数据不存在&#x27;</span>]</span><br><span class="line">        <span class="keyword">if</span> (plt_4==[]): plt_4=[<span class="string">&#x27;数据不存在&#x27;</span>]</span><br><span class="line">        <span class="keyword">if</span> (plt_5==[]): plt_5=[<span class="string">&#x27;数据不存在&#x27;</span>]</span><br><span class="line">        a=plt_3+plt_4+plt_5+plt_1+plt_2</span><br><span class="line">        <span class="keyword">return</span> a</span><br><span class="line">    <span class="keyword">except</span> Exception <span class="keyword">as</span> e:</span><br><span class="line">        <span class="built_in">print</span>(e)</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;信息提取失败&quot;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">main</span>():</span></span><br><span class="line">    goods = <span class="string">&quot;手机&quot;</span>    <span class="comment">#设置爬取的商品</span></span><br><span class="line">    depth = <span class="number">2</span>         <span class="comment">#设置爬取的页数</span></span><br><span class="line">    start_url = <span class="string">&quot;https://s.taobao.com/search?q=&quot;</span> + goods</span><br><span class="line">    start_url_1 = <span class="string">&quot;https://detail.tmall.com/item.htm?id=&quot;</span></span><br><span class="line"></span><br><span class="line">    </span><br><span class="line">    <span class="keyword">try</span>:</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;正在与数据库建立连接...&quot;</span>)</span><br><span class="line">        db = pymysql.connect( host=<span class="string">&quot;localhost&quot;</span>,user=<span class="string">&quot;root&quot;</span>,passwd=<span class="string">&quot;root&quot;</span>,db=<span class="string">&quot;taobao&quot;</span>,charset=<span class="string">&quot;utf8&quot;</span>)   <span class="comment">#与本地数据库建立连接，db连接数据库taobao，charset连接后使用的编码为utf8</span></span><br><span class="line">        cursor = db.cursor()            <span class="comment">#获取游标对象</span></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;数据库连接成功！&quot;</span>)</span><br><span class="line">    <span class="keyword">except</span> Exception <span class="keyword">as</span> e:</span><br><span class="line">        <span class="built_in">print</span>(e)</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;数据库无法连接!&quot;</span>)</span><br><span class="line"></span><br><span class="line">        </span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(depth):</span><br><span class="line">        <span class="keyword">try</span>:</span><br><span class="line">            url = start_url + <span class="string">&quot;&amp;s=&quot;</span> + <span class="built_in">str</span>(<span class="number">44</span> * i)    <span class="comment">#构建搜索页URL</span></span><br><span class="line">            headers = &#123;                     <span class="comment">#user-agent是消息头，模仿浏览器访问淘宝；cookie是身份令牌</span></span><br><span class="line">                <span class="string">&quot;user-agent&quot;</span>: <span class="string">&quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36&quot;</span>,</span><br><span class="line">                <span class="string">&quot;cookie&quot;</span>: “这里填自己的cookie”</span><br><span class="line">                &#125;</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;正在与网页建立连接...&quot;</span>)</span><br><span class="line">            html = requests.get(url, headers=headers)    <span class="comment">#与网页建立连接</span></span><br><span class="line">            b=re.findall(<span class="string">r&#x27;\&quot;auctionNids\&quot;\:\[(.*?)\]&#x27;</span>,html.text)   <span class="comment">#提取某页所有商品的标志，用于爬取商品的详细属性</span></span><br><span class="line">            c=<span class="built_in">str</span>(b)</span><br><span class="line">            b=re.findall(<span class="string">r&#x27;\d+&#x27;</span>,c)</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;在第&quot;</span>+<span class="built_in">str</span>(i+<span class="number">1</span>)+<span class="string">&quot;页爬取到&quot;</span>+<span class="built_in">str</span>(<span class="built_in">len</span>(b))+<span class="string">&quot;个商品&quot;</span>)</span><br><span class="line">            <span class="keyword">for</span> n <span class="keyword">in</span> <span class="built_in">range</span>(<span class="built_in">len</span>(b)):</span><br><span class="line">                <span class="keyword">try</span>:</span><br><span class="line">                    url_1 = start_url_1 + b[n]     <span class="comment">#根据前面爬取的商品标志构建URL，更换页面，爬取商品详细属性</span></span><br><span class="line">                    html_1 = requests.get(url_1, headers=headers)   </span><br><span class="line">                    <span class="built_in">print</span>(<span class="string">&quot;正在爬取商品&quot;</span>+<span class="built_in">str</span>(n+<span class="number">1</span>))</span><br><span class="line">                    a=parsePage_1(html_1.text)       <span class="comment">#将爬取的商品详尽页传入parsePage_1，用于提取商品属性，并返回到a</span></span><br><span class="line">                    <span class="built_in">print</span>(<span class="string">&quot;商品&quot;</span>+<span class="built_in">str</span>(n+<span class="number">1</span>)+<span class="string">&quot;信息爬取成功！&quot;</span>)</span><br><span class="line">                    </span><br><span class="line">                    <span class="keyword">try</span>:</span><br><span class="line">                        </span><br><span class="line">                        sql = <span class="string">&quot;INSERT INTO sj(id,pp,cpmc,cpu,hzsxt,fbl)VALUES (%s,&#x27;%s&#x27;,&#x27;%s&#x27;,&#x27;%s&#x27;,&#x27;%s&#x27;,&#x27;%s&#x27;)&quot;</span>%(n,a[<span class="number">0</span>],a[<span class="number">1</span>],a[<span class="number">2</span>],a[<span class="number">3</span>],a[<span class="number">4</span>])    <span class="comment">#构建SQL语句，为导入到数据库做准备</span></span><br><span class="line">                        cursor.execute(sql)        <span class="comment">#开始导入</span></span><br><span class="line">                        db.commit()             <span class="comment">#保存</span></span><br><span class="line">                        <span class="built_in">print</span>(<span class="string">&quot;数据已写入数据库&quot;</span>)</span><br><span class="line">                    <span class="keyword">except</span> Exception <span class="keyword">as</span> e:</span><br><span class="line">                        <span class="built_in">print</span> (e)</span><br><span class="line">                        db.rollback()    <span class="comment">#如果发送错误进行数据回滚</span></span><br><span class="line">                        <span class="built_in">print</span>(<span class="string">&quot;数据库写入失败！&quot;</span>)</span><br><span class="line">                <span class="keyword">except</span> Exception <span class="keyword">as</span> e:</span><br><span class="line">                    <span class="built_in">print</span> (e)</span><br><span class="line">                    <span class="keyword">continue</span></span><br><span class="line">    </span><br><span class="line">        <span class="keyword">except</span> Exception <span class="keyword">as</span> e:</span><br><span class="line">            <span class="built_in">print</span>(e)</span><br><span class="line">            <span class="keyword">continue</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">try</span>:</span><br><span class="line">        db.close()    <span class="comment">#关闭数据库连接</span></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;数据库已关闭&quot;</span>)</span><br><span class="line">    <span class="keyword">except</span> Exception <span class="keyword">as</span> e:</span><br><span class="line">        <span class="built_in">print</span>(e)</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;数据库关闭失败！&quot;</span>)</span><br><span class="line">    </span><br><span class="line">main()</span><br></pre>
<h4 id="爬取结果"><a class="markdownIt-Anchor" href="#爬取结果"></a> 爬取结果</h4>
<p><img src="https://gitee.com/baikaihua/tochang/raw/master/QQ%E6%88%AA%E5%9B%BE20211213170200.png" alt="" / srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="https://gitee.com/baikaihua/tochang/raw/master/QQ%E6%88%AA%E5%9B%BE20211213170200.png" class="lozad post-image"></p>
<p><img src="https://gitee.com/baikaihua/tochang/raw/master/QQ%E6%88%AA%E5%9B%BE20211213170420.png" alt="" / srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="https://gitee.com/baikaihua/tochang/raw/master/QQ%E6%88%AA%E5%9B%BE20211213170420.png" class="lozad post-image"></p>
<p>​		通过Navicat for MySQL我们看到爬取的数据已经在数据库中了，但存在无效数据“数据不存在”，这是因为我们正则表达式提取不到数据。</p>
<p>​	原因如下：</p>
<ol>
<li>淘宝页面的不统一</li>
<li>商品参数命名的混乱</li>
<li>页面访问过快触发反爬机制</li>
</ol>
<h3 id="实验总结"><a class="markdownIt-Anchor" href="#实验总结"></a> 实验总结</h3>
<p>​		我们的目的是通过爬虫程序自动化浏览网络中的信息，并将我们需要的数据保存到数据库。</p>
<p>​	爬虫的代码可以看做三个部分：</p>
<ol>
<li>
<p>页面连接模块</p>
</li>
<li>
<p>数据获取模块</p>
</li>
<li>
<p>数据库操作模块</p>
</li>
</ol>
<p>​        首先用request库提供的方法来进行本机与网站的连接，将所连接的网页的源代码下载下来；再通过正则表达式来提取自己想要的数据；接着通过pymysql库进行与数据库的连接，将爬取到的数据储存到数据库中。</p>
<h3 id="学习感悟"><a class="markdownIt-Anchor" href="#学习感悟"></a> 学习感悟</h3>
<p>​		大数据时代，各个领域的飞速变化，身处校园即将面向社会的我们应从知识和能力两方面储备，既要学好专业课程知识又需了解时代发展方向、掌握社会发展脉搏，为自己以后踏入社会做好准备。</p>
<p>​		我们通过学习网络爬虫可以获取更多的数据源，且面对网络上海量的信息，通过爬虫程序可以提取出我们想要的数据，实现信息的高效获取。</p>
<p>​		这次实验，即实践了课本上的内容，又对我们的知识进行查漏补缺，让我们对爬虫有了新的认识与理解。通过实验对自己的不足之处也有了了解，深刻认识到打牢基础的重要性，还需完善自己的知识体系，加强自己对事物的分析能力的训练。</p>
<p>​		本次实验锻炼了我们的动手能力，也进一步巩固了对此专业课的理解。在实验中发现问题，独立思考，最终解决问题，从而也就加深我对课本理论知识的理解，达到了“双赢”的效果。</p>
<p>​		在大数据时代，我国青年一代比将大有可为，也必将大有作为。广大青年要勇敢肩负起时代赋予的责任，志存高远，脚踏实地，努力学习科学文化知识，用知识武装自己，为实现中华民族伟大复兴的中国梦奋斗终生！</p>

  </div>
  <div>
    
      <div 
        class="post-note note-warning copyright" 
        style="margin-top: 42px">
        <p>
          <span style="font-weight: bold;">作者：</span><a 
            target="_blank" 
            rel="nofollow noopener noreferrer" 
            href="/about">
            白开画 and 徐威
          </a>
        </p>
        <p>
          <span style="font-weight: bold;">文章链接：</span><a 
            target="_blank" 
            rel="nofollow noopener noreferrer" 
            href="http://www.baikaihua.gitee.io/2021/12/13/python%E7%88%AC%E5%8F%96%E6%B7%98%E5%AE%9D%E5%95%86%E5%93%81/index.html">
            http://www.baikaihua.gitee.io/2021/12/13/python%E7%88%AC%E5%8F%96%E6%B7%98%E5%AE%9D%E5%95%86%E5%93%81/index.html
          </a>
        </p>
        <p><span style="font-weight: bold;">版权声明：</span>本博客所有文章除特别声明外，均采用<a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">CC BY-NC-SA 4.0 协议</a>。转载请注明出处！</p>
      </div>
    
  </div>
</article>
<div class="nav">
  
  
    <div class="nav-item-next">
      <a 
        href="/2021/12/13/PHP%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C/index.html" 
        class="nav-link">
        <div>
          <div class="nav-label">下一篇</div>
          
            <div class="nav-title">PHP字符串常用操作 </div>
          
        </div>
        <i class="iconfont icon-right nav-next-icon"></i>
      </a>
    </div>
  
</div>

  <div 
    class="card card-content comment-card" 
    style="margin-top: 16px;">
    <div class="comment-card-title">评论</div>
    
  <div id="vcomments"></div>
  
  <script>
    loadScript("//unpkg.com/valine/dist/Valine.min.js");
    var oldLoadVa = window.onload;
    window.onload = function () {
      oldLoadVa && oldLoadVa();
      new Valine({
        el: '#vcomments',
        appId: 'C2ey5RzCeMGvikIpezIqS1fW-gzGzoHsz',
        appKey: 'yNj1HPpcuT4nFzgDG1hfxiwX',
        placeholder: 'Just go go',
        path: window.location.pathname,
        avatar: 'mp',
        meta: ["nick","mail","link"],
        pageSize: '10',
        lang: '',
        visitor: 'false',
        highlight: true,
        recordIP: false,
        
        
        
        enableQQ: 'false',
        requiredFields: [],
      });
    };
  </script>

  </div>

<div 
  class="card card-content toc-card" 
  id="mobiletoc">
  <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9E%E9%AA%8C%E5%86%85%E5%AE%B9"><span class="toc-number">1.</span> <span class="toc-text"> 实验内容</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9E%E9%AA%8C%E7%9B%AE%E7%9A%84"><span class="toc-number">2.</span> <span class="toc-text"> 实验目的</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9E%E9%AA%8C%E7%8E%AF%E5%A2%83"><span class="toc-number">3.</span> <span class="toc-text"> 实验环境</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9E%E9%AA%8C%E4%BB%BB%E5%8A%A1%E5%88%97%E8%A1%A8"><span class="toc-number">4.</span> <span class="toc-text"> 实验任务列表</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9E%E9%AA%8C%E8%BF%87%E7%A8%8B"><span class="toc-number">5.</span> <span class="toc-text"> 实验过程</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AE%89%E8%A3%85request%E5%BA%93%E5%92%8Cpymysql%E5%BA%93"><span class="toc-number">5.1.</span> <span class="toc-text"> 安装request库和pymysql库</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%B7%98%E5%AE%9D%E7%9A%84url%E5%88%86%E6%9E%90"><span class="toc-number">5.2.</span> <span class="toc-text"> 淘宝的URL分析</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E9%A1%B5%E9%9D%A2%E4%BF%A1%E6%81%AF%E6%A8%A1%E5%9D%97"><span class="toc-number">5.3.</span> <span class="toc-text"> 获取页面信息模块</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%AF%B7%E6%B1%82%E5%A4%B4%E4%B8%8Ecooker"><span class="toc-number">5.4.</span> <span class="toc-text"> 请求头与cooker</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%BB%BA%E7%AB%8B%E7%BD%91%E9%A1%B5%E8%BF%9E%E6%8E%A5"><span class="toc-number">5.5.</span> <span class="toc-text"> 建立网页连接</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%88%86%E6%9E%90%E6%B7%98%E5%AE%9D%E6%90%9C%E7%B4%A2%E5%90%8E%E7%9A%84html"><span class="toc-number">5.6.</span> <span class="toc-text"> 分析淘宝搜索后的HTML</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#url%E7%9A%84%E4%BA%8C%E6%AC%A1%E6%9E%84%E5%BB%BA%E4%B8%8E%E8%BF%9E%E6%8E%A5"><span class="toc-number">5.7.</span> <span class="toc-text"> URL的二次构建与连接</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%95%86%E5%93%81%E8%AF%A6%E7%BB%86%E9%A1%B5%E9%9D%A2html%E7%9A%84%E5%88%86%E6%9E%90%E4%B8%8E%E6%95%B0%E6%8D%AE%E6%8F%90%E5%8F%96"><span class="toc-number">5.8.</span> <span class="toc-text"> 商品详细页面HTML的分析与数据提取</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#mysql%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9A%84%E8%BF%9E%E6%8E%A5%E4%B8%8E%E5%85%B3%E9%97%AD"><span class="toc-number">5.9.</span> <span class="toc-text"> MySQL数据库的连接与关闭</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E8%BF%9E%E6%8E%A5"><span class="toc-number">5.9.1.</span> <span class="toc-text"> 连接</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%85%B3%E9%97%AD"><span class="toc-number">5.9.2.</span> <span class="toc-text"> 关闭</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9A%84%E5%87%86%E5%A4%87"><span class="toc-number">5.10.</span> <span class="toc-text"> 数据库的准备</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E7%9A%84%E6%8F%92%E5%85%A5"><span class="toc-number">5.11.</span> <span class="toc-text"> 数据的插入</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%85%A8%E9%83%A8%E4%BB%A3%E7%A0%81"><span class="toc-number">5.12.</span> <span class="toc-text"> 全部代码</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%88%AC%E5%8F%96%E7%BB%93%E6%9E%9C"><span class="toc-number">5.13.</span> <span class="toc-text"> 爬取结果</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9E%E9%AA%8C%E6%80%BB%E7%BB%93"><span class="toc-number">6.</span> <span class="toc-text"> 实验总结</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AD%A6%E4%B9%A0%E6%84%9F%E6%82%9F"><span class="toc-number">7.</span> <span class="toc-text"> 学习感悟</span></a></li></ol>
</div></main>
            <aside class="left-column">
              
              <div class="card card-author">
                
  <img 
    src="https://gitee.com/baikaihua/tochang/raw/master/src=http___b-ssl.duitang.com_uploads_item_201607_07_20160707163006_EnLeG.jpeg&refer=http___b-ssl.duitang.jpg" 
    class="author-img" 
    alt="author avatar">

<p class="author-name">白开画 and 徐威</p>
<p class="author-description">欢迎！</p>
<div class="author-message">
  <a 
    class="author-posts-count" 
    href="/archives">
    <span>33</span>
    <span>文章</span>
  </a>
  <a 
    class="author-categories-count" 
    href="/categories">
    <span>11</span>
    <span>分类</span>
  </a>
  <a 
    class="author-tags-count" 
    href="/tags">
    <span>37</span>
    <span>标签</span>
  </a>
</div>

              </div>
               <div class="sticky-tablet">
  
  
    <article class="display-when-two-columns spacer">
      <div class="card card-content toc-card">
        <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9E%E9%AA%8C%E5%86%85%E5%AE%B9"><span class="toc-number">1.</span> <span class="toc-text"> 实验内容</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9E%E9%AA%8C%E7%9B%AE%E7%9A%84"><span class="toc-number">2.</span> <span class="toc-text"> 实验目的</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9E%E9%AA%8C%E7%8E%AF%E5%A2%83"><span class="toc-number">3.</span> <span class="toc-text"> 实验环境</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9E%E9%AA%8C%E4%BB%BB%E5%8A%A1%E5%88%97%E8%A1%A8"><span class="toc-number">4.</span> <span class="toc-text"> 实验任务列表</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9E%E9%AA%8C%E8%BF%87%E7%A8%8B"><span class="toc-number">5.</span> <span class="toc-text"> 实验过程</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AE%89%E8%A3%85request%E5%BA%93%E5%92%8Cpymysql%E5%BA%93"><span class="toc-number">5.1.</span> <span class="toc-text"> 安装request库和pymysql库</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%B7%98%E5%AE%9D%E7%9A%84url%E5%88%86%E6%9E%90"><span class="toc-number">5.2.</span> <span class="toc-text"> 淘宝的URL分析</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E9%A1%B5%E9%9D%A2%E4%BF%A1%E6%81%AF%E6%A8%A1%E5%9D%97"><span class="toc-number">5.3.</span> <span class="toc-text"> 获取页面信息模块</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%AF%B7%E6%B1%82%E5%A4%B4%E4%B8%8Ecooker"><span class="toc-number">5.4.</span> <span class="toc-text"> 请求头与cooker</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%BB%BA%E7%AB%8B%E7%BD%91%E9%A1%B5%E8%BF%9E%E6%8E%A5"><span class="toc-number">5.5.</span> <span class="toc-text"> 建立网页连接</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%88%86%E6%9E%90%E6%B7%98%E5%AE%9D%E6%90%9C%E7%B4%A2%E5%90%8E%E7%9A%84html"><span class="toc-number">5.6.</span> <span class="toc-text"> 分析淘宝搜索后的HTML</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#url%E7%9A%84%E4%BA%8C%E6%AC%A1%E6%9E%84%E5%BB%BA%E4%B8%8E%E8%BF%9E%E6%8E%A5"><span class="toc-number">5.7.</span> <span class="toc-text"> URL的二次构建与连接</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%95%86%E5%93%81%E8%AF%A6%E7%BB%86%E9%A1%B5%E9%9D%A2html%E7%9A%84%E5%88%86%E6%9E%90%E4%B8%8E%E6%95%B0%E6%8D%AE%E6%8F%90%E5%8F%96"><span class="toc-number">5.8.</span> <span class="toc-text"> 商品详细页面HTML的分析与数据提取</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#mysql%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9A%84%E8%BF%9E%E6%8E%A5%E4%B8%8E%E5%85%B3%E9%97%AD"><span class="toc-number">5.9.</span> <span class="toc-text"> MySQL数据库的连接与关闭</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E8%BF%9E%E6%8E%A5"><span class="toc-number">5.9.1.</span> <span class="toc-text"> 连接</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%85%B3%E9%97%AD"><span class="toc-number">5.9.2.</span> <span class="toc-text"> 关闭</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9A%84%E5%87%86%E5%A4%87"><span class="toc-number">5.10.</span> <span class="toc-text"> 数据库的准备</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E7%9A%84%E6%8F%92%E5%85%A5"><span class="toc-number">5.11.</span> <span class="toc-text"> 数据的插入</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%85%A8%E9%83%A8%E4%BB%A3%E7%A0%81"><span class="toc-number">5.12.</span> <span class="toc-text"> 全部代码</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%88%AC%E5%8F%96%E7%BB%93%E6%9E%9C"><span class="toc-number">5.13.</span> <span class="toc-text"> 爬取结果</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9E%E9%AA%8C%E6%80%BB%E7%BB%93"><span class="toc-number">6.</span> <span class="toc-text"> 实验总结</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AD%A6%E4%B9%A0%E6%84%9F%E6%82%9F"><span class="toc-number">7.</span> <span class="toc-text"> 学习感悟</span></a></li></ol>
      </div>
    </article>
  
  
  <article class="card card-content categories-widget">
    <div class="categories-card">
  <div class="categories-header">
    <i 
      class="iconfont icon-fenlei" 
      style="padding-right: 2px;">
    </i>分类
  </div>
  <div class="categories-list">
    
      <a href="/categories/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BD%91%E7%BB%9C/">
        <div class="categories-list-item">
          计算机网络
          <span class="categories-list-item-badge">5</span>
        </div>
      </a>
    
      <a href="/categories/%E6%95%B0%E6%8D%AE%E5%BA%93/">
        <div class="categories-list-item">
          数据库
          <span class="categories-list-item-badge">3</span>
        </div>
      </a>
    
      <a href="/categories/PHP/">
        <div class="categories-list-item">
          PHP
          <span class="categories-list-item-badge">14</span>
        </div>
      </a>
    
      <a href="/categories/Windows%E7%B3%BB%E7%BB%9F/">
        <div class="categories-list-item">
          Windows系统
          <span class="categories-list-item-badge">1</span>
        </div>
      </a>
    
      <a href="/categories/Python/">
        <div class="categories-list-item">
          Python
          <span class="categories-list-item-badge">1</span>
        </div>
      </a>
    
      <a href="/categories/Linux/">
        <div class="categories-list-item">
          Linux
          <span class="categories-list-item-badge">1</span>
        </div>
      </a>
    
      <a href="/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95/">
        <div class="categories-list-item">
          数据结构与算法
          <span class="categories-list-item-badge">2</span>
        </div>
      </a>
    
      <a href="/categories/%E7%BD%91%E7%BB%9C/">
        <div class="categories-list-item">
          网络
          <span class="categories-list-item-badge">1</span>
        </div>
      </a>
    
      <a href="/categories/%E8%B7%AF%E7%94%B1%E4%BA%A4%E6%8D%A2/">
        <div class="categories-list-item">
          路由交换
          <span class="categories-list-item-badge">3</span>
        </div>
      </a>
    
      <a href="/categories/%E8%BD%AC%E8%BD%BD/">
        <div class="categories-list-item">
          转载
          <span class="categories-list-item-badge">1</span>
        </div>
      </a>
    
      <a href="/categories/python/">
        <div class="categories-list-item">
          python
          <span class="categories-list-item-badge">1</span>
        </div>
      </a>
    
  </div>
</div>
  </article>
  
  <article class="card card-content tags-widget">
    <div class="tags-card">
  <div class="tags-header">
    <i 
      class="iconfont icon-biaoqian" 
      style="padding-right: 2px;">
    </i>热门标签
  </div>
  <div class="tags-list">
    
      <a 
        href="/tags/%E7%99%BD%E5%BC%80%E7%94%BB/" 
        title="白开画">
        <div class="tags-list-item">白开画</div>
      </a>
    
      <a 
        href="/tags/%E5%BE%90%E5%A8%81/" 
        title="徐威">
        <div class="tags-list-item">徐威</div>
      </a>
    
      <a 
        href="/tags/PHP/" 
        title="PHP">
        <div class="tags-list-item">PHP</div>
      </a>
    
      <a 
        href="/tags/web%E8%84%9A%E6%9C%AC/" 
        title="web脚本">
        <div class="tags-list-item">web脚本</div>
      </a>
    
      <a 
        href="/tags/%E7%BD%91%E7%BB%9C/" 
        title="网络">
        <div class="tags-list-item">网络</div>
      </a>
    
      <a 
        href="/tags/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BD%91%E7%BB%9C/" 
        title="计算机网络">
        <div class="tags-list-item">计算机网络</div>
      </a>
    
      <a 
        href="/tags/%E5%8D%8E%E4%B8%BA/" 
        title="华为">
        <div class="tags-list-item">华为</div>
      </a>
    
      <a 
        href="/tags/%E5%8D%8E%E4%B8%BA%E8%AE%A4%E8%AF%81/" 
        title="华为认证">
        <div class="tags-list-item">华为认证</div>
      </a>
    
      <a 
        href="/tags/%E8%B7%AF%E7%94%B1%E4%BA%A4%E6%8D%A2/" 
        title="路由交换">
        <div class="tags-list-item">路由交换</div>
      </a>
    
      <a 
        href="/tags/GVRP/" 
        title="GVRP">
        <div class="tags-list-item">GVRP</div>
      </a>
    
      <a 
        href="/tags/VLAN/" 
        title="VLAN">
        <div class="tags-list-item">VLAN</div>
      </a>
    
      <a 
        href="/tags/%E5%9F%BA%E7%A1%80/" 
        title="基础">
        <div class="tags-list-item">基础</div>
      </a>
    
      <a 
        href="/tags/ip/" 
        title="ip">
        <div class="tags-list-item">ip</div>
      </a>
    
      <a 
        href="/tags/%E5%A4%A7%E6%95%B0/" 
        title="大数">
        <div class="tags-list-item">大数</div>
      </a>
    
      <a 
        href="/tags/python/" 
        title="python">
        <div class="tags-list-item">python</div>
      </a>
    
      <a 
        href="/tags/%E5%91%BD%E4%BB%A4%E6%8F%90%E7%A4%BA%E7%AC%A6/" 
        title="命令提示符">
        <div class="tags-list-item">命令提示符</div>
      </a>
    
  </div>
</div>
  </article>
  
  
</div>
            </aside>
            <aside class="right-column">
              <div class="sticky-widescreen">
  
  
    <article class="card card-content toc-card">
      <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9E%E9%AA%8C%E5%86%85%E5%AE%B9"><span class="toc-number">1.</span> <span class="toc-text"> 实验内容</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9E%E9%AA%8C%E7%9B%AE%E7%9A%84"><span class="toc-number">2.</span> <span class="toc-text"> 实验目的</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9E%E9%AA%8C%E7%8E%AF%E5%A2%83"><span class="toc-number">3.</span> <span class="toc-text"> 实验环境</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9E%E9%AA%8C%E4%BB%BB%E5%8A%A1%E5%88%97%E8%A1%A8"><span class="toc-number">4.</span> <span class="toc-text"> 实验任务列表</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9E%E9%AA%8C%E8%BF%87%E7%A8%8B"><span class="toc-number">5.</span> <span class="toc-text"> 实验过程</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AE%89%E8%A3%85request%E5%BA%93%E5%92%8Cpymysql%E5%BA%93"><span class="toc-number">5.1.</span> <span class="toc-text"> 安装request库和pymysql库</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%B7%98%E5%AE%9D%E7%9A%84url%E5%88%86%E6%9E%90"><span class="toc-number">5.2.</span> <span class="toc-text"> 淘宝的URL分析</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E9%A1%B5%E9%9D%A2%E4%BF%A1%E6%81%AF%E6%A8%A1%E5%9D%97"><span class="toc-number">5.3.</span> <span class="toc-text"> 获取页面信息模块</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%AF%B7%E6%B1%82%E5%A4%B4%E4%B8%8Ecooker"><span class="toc-number">5.4.</span> <span class="toc-text"> 请求头与cooker</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%BB%BA%E7%AB%8B%E7%BD%91%E9%A1%B5%E8%BF%9E%E6%8E%A5"><span class="toc-number">5.5.</span> <span class="toc-text"> 建立网页连接</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%88%86%E6%9E%90%E6%B7%98%E5%AE%9D%E6%90%9C%E7%B4%A2%E5%90%8E%E7%9A%84html"><span class="toc-number">5.6.</span> <span class="toc-text"> 分析淘宝搜索后的HTML</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#url%E7%9A%84%E4%BA%8C%E6%AC%A1%E6%9E%84%E5%BB%BA%E4%B8%8E%E8%BF%9E%E6%8E%A5"><span class="toc-number">5.7.</span> <span class="toc-text"> URL的二次构建与连接</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%95%86%E5%93%81%E8%AF%A6%E7%BB%86%E9%A1%B5%E9%9D%A2html%E7%9A%84%E5%88%86%E6%9E%90%E4%B8%8E%E6%95%B0%E6%8D%AE%E6%8F%90%E5%8F%96"><span class="toc-number">5.8.</span> <span class="toc-text"> 商品详细页面HTML的分析与数据提取</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#mysql%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9A%84%E8%BF%9E%E6%8E%A5%E4%B8%8E%E5%85%B3%E9%97%AD"><span class="toc-number">5.9.</span> <span class="toc-text"> MySQL数据库的连接与关闭</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E8%BF%9E%E6%8E%A5"><span class="toc-number">5.9.1.</span> <span class="toc-text"> 连接</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%85%B3%E9%97%AD"><span class="toc-number">5.9.2.</span> <span class="toc-text"> 关闭</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9A%84%E5%87%86%E5%A4%87"><span class="toc-number">5.10.</span> <span class="toc-text"> 数据库的准备</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E7%9A%84%E6%8F%92%E5%85%A5"><span class="toc-number">5.11.</span> <span class="toc-text"> 数据的插入</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%85%A8%E9%83%A8%E4%BB%A3%E7%A0%81"><span class="toc-number">5.12.</span> <span class="toc-text"> 全部代码</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%88%AC%E5%8F%96%E7%BB%93%E6%9E%9C"><span class="toc-number">5.13.</span> <span class="toc-text"> 爬取结果</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9E%E9%AA%8C%E6%80%BB%E7%BB%93"><span class="toc-number">6.</span> <span class="toc-text"> 实验总结</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AD%A6%E4%B9%A0%E6%84%9F%E6%82%9F"><span class="toc-number">7.</span> <span class="toc-text"> 学习感悟</span></a></li></ol>
    </article>
  
  
  <article class="card card-content">
    <div class="recent-posts-card">
  <div class="recent-posts-header">
    <i 
      class="iconfont icon-wenzhang_huaban" 
      style="padding-right: 2px;">
    </i>最近文章
  </div>
  <div class="recent-posts-list">
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-12-13</div>
        <a href="/2021/12/13/python%E7%88%AC%E5%8F%96%E6%B7%98%E5%AE%9D%E5%95%86%E5%93%81/index.html"><div class="recent-posts-item-content">python爬取淘宝商品</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-12-13</div>
        <a href="/2021/12/13/PHP%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C/index.html"><div class="recent-posts-item-content">PHP字符串常用操作</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-11-29</div>
        <a href="/2021/11/29/PHP%E8%AE%A4%E8%AF%86%E5%AD%97%E7%AC%A6%E4%B8%B2/index.html"><div class="recent-posts-item-content">PHP认识字符串</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-11-21</div>
        <a href="/2021/11/21/PHP%E5%87%BD%E6%95%B0%E7%9A%84%E5%BA%94%E7%94%A8/index.html"><div class="recent-posts-item-content">PHP函数的应用</div></a>
      </div>
    
  </div>
</div>
  </article>
  
  
</div>
            </aside>
          </div>
        </div>
      </div>
    </div>
     
    <footer class="footer">
  <div class="footer-container">
    <div>
      <div class="footer-dsc">
        <span>
          Copyright ©
          
            2021 -
          
          2021
        </span>
        &nbsp;
        <a 
          href="/" 
          class="footer-link">
          程序小站
        </a>
      </div>
    </div>

    
      <div class="footer-dsc">
        
          Powered by
          <a 
            href="https://hexo.io/" 
            class="footer-link" 
            target="_blank" 
            rel="nofollow noopener noreferrer">
            &nbsp;Hexo
          </a>
        
        
          <span>&nbsp;|&nbsp;</span>
        
        
          Theme -
          <a 
            href="https://github.com/theme-kaze" 
            class="footer-link" 
            target="_blank"
            rel="nofollow noopener noreferrer">
            &nbsp;Kaze
          </a>
        
      </div>
    
    
    
    
      <div class="footer-dsc">
        
          本站总访问量<span id="busuanzi_value_site_pv"></span>次
        
        
          <span>&nbsp;|&nbsp;</span>
        
        
          本站总访客数<span id="busuanzi_value_site_uv"></span>次
        
      </div>
      
    
</footer> 
    
  <a 
    role="button" 
    id="scrollbutton" 
    class="basebutton" 
    aria-label="回到顶部">
    <i class="iconfont icon-arrowleft button-icon"></i>
  </a>

<a 
  role="button" 
  id="menubutton" 
  class="basebutton">
  <i class="iconfont icon-menu button-icon"></i>
</a>
<a 
  role="button" 
  id="popbutton" 
  class="basebutton" 
  aria-label="控制中心">
  <i class="iconfont icon-expand button-icon"></i>
</a>
<a 
  role="button" 
  id="darkbutton" 
  class="basebutton darkwidget" 
  aria-label="夜色模式">
  <i class="iconfont icon-weather button-icon"></i>
</a>
<a 
  role="button" 
  id="searchbutton" 
  class="basebutton searchwidget" 
  aria-label="搜索">
  <i class="iconfont icon-search button-icon"></i>
</a> 
     
     
      

  
  
    
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/katex@0.11.1/dist/katex.min.css">

  
 
     
     
      <script>
  var addImgLayout = function () {
    var img = document.querySelectorAll('.post-content img')
    var i
    for (i = 0; i < img.length; i++) {
      var wrapper = document.createElement('a')
      wrapper.setAttribute('href', img[i].getAttribute('data-src'))
      wrapper.setAttribute('aria-label', 'illustration')
      wrapper.style.cssText =
        'width: 100%; display: flex; justify-content: center;'
      if (img[i].alt) wrapper.dataset.caption = img[i].alt
      wrapper.dataset.nolink = true
      img[i].before(wrapper)
      wrapper.append(img[i])
      var divWrap = document.createElement('div')
      divWrap.classList.add('gallery')
      wrapper.before(divWrap)
      divWrap.append(wrapper)
    }
    baguetteBox.run('.gallery')
  }
</script>
<script>
  loadScript(
    "/js/lib/lightbox/baguetteBox.min.js",
    addImgLayout
  )
</script>
 
     
     
    <script src="/js/main.js"></script> 
    
      <script> 
        loadScript('/js/lib/busuanzi.min.js') 
      </script>
     
    
      <script>
        var addLazyload = function () {
          var observer = lozad('.lozad', {
            load: function (el) {
              el.srcset = el.getAttribute('data-src')
            },
            loaded: function (el) {
              el.classList.add('loaded')
            },
          })
          observer.observe()
        }
      </script>
      <script>
        loadScript('/js/lib/lozad.min.js', addLazyload)
      </script>
     
    
    
      <script>
        setTimeout(() => {localSearch("search.json")}, 0)
      </script>
    
  </body>
</html>
